package com.spic.business.hidden.place.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.hidden.place.domain.HiddenPlace;
import com.spic.business.hidden.place.domain.vo.TreeSelect;
import com.spic.business.hidden.place.service.IHiddenPlaceService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 隐患地点Controller
 *
 * @author yangjie
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/hidden/place")
public class HiddenPlaceController extends BaseController
{
    @Autowired
    private IHiddenPlaceService hiddenPlaceService;

    /**
     * 查询隐患地点列表
     */
    @PreAuthorize(hasPermi = "business/hidden:place:list")
    @GetMapping("/list")
    public AjaxResult list(HiddenPlace hiddenPlace)
    {
        List<HiddenPlace> list = hiddenPlaceService.selectHiddenPlaceList(hiddenPlace);
        return AjaxResult.success(list);
    }

    /**
     * 查询树列表
     * @param hiddenPlace
     * @return
     */
    @GetMapping("/placetreeselect")
    public AjaxResult placetreeselect(HiddenPlace hiddenPlace)
    {
        List<HiddenPlace> list = hiddenPlaceService.selectHiddenPlaceList(hiddenPlace);
        List<TreeSelect> hiddenPlaces = hiddenPlaceService.buildDeptTreeSelect(list);
        return AjaxResult.success(hiddenPlaces);
    }

    /**
     * 根据id查询隐患地点数据
     * @param hiddenPlace
     * @return
     */
    @GetMapping("/getplacebyhiddebytype")
    public AjaxResult getPlacebyHiddeType(HiddenPlace hiddenPlace)
    {
        List<HiddenPlace> hiddenTypeList = hiddenPlaceService.selectPlacebyHiddeType(hiddenPlace);
        return AjaxResult.success(hiddenTypeList);
    }
    /**
     * 根据一级传的id并把它当作父Id查询二级数据
     * @param parentId
     * @return
     */
    @GetMapping("/getplaceByParentid/{parentId}")
    public AjaxResult getplaceByParentid(@PathVariable("parentId") String parentId)
    {
        List<HiddenPlace> hiddenTypeList = hiddenPlaceService.selectgetplaceByParentid(parentId);
        return AjaxResult.success(hiddenTypeList);
    }

    /**
     * 导出隐患地点列表
     */
    @PreAuthorize(hasPermi = "business/hidden:place:export")
    @Log(title = "隐患地点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiddenPlace hiddenPlace) throws IOException
    {
        List<HiddenPlace> list = hiddenPlaceService.selectHiddenPlaceList(hiddenPlace);
        ExcelUtil<HiddenPlace> util = new BusinessExcelUtil<HiddenPlace>(HiddenPlace.class);
        util.exportExcel(response, list, "隐患地点数据");
    }

    /**
     * 获取隐患地点详细信息
     */
    @PreAuthorize(hasPermi = "business/hidden:place:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(hiddenPlaceService.selectHiddenPlaceById(id));
    }

    /**
     * 新增隐患地点
     */
    @PreAuthorize(hasPermi = "business/hidden:place:add")
    @Log(title = "隐患地点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiddenPlace hiddenPlace)
    {
        return toAjax(hiddenPlaceService.insertHiddenPlace(hiddenPlace));
    }

    /**
     * 修改隐患地点
     */
    @PreAuthorize(hasPermi = "business/hidden:place:edit")
    @Log(title = "隐患地点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HiddenPlace hiddenPlace)
    {
        return toAjax(hiddenPlaceService.updateHiddenPlace(hiddenPlace));
    }

    /**
     * 删除隐患地点
     */
    @PreAuthorize(hasPermi = "business/hidden:place:remove")
    @Log(title = "隐患地点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(hiddenPlaceService.deleteHiddenPlaceByIds(ids));
    }


    /**
     * 二级联动数据加载,以及页面数据解析
     * @param hiddenPlace
     * @return
     */
    @GetMapping("/getHiddePlaceByParentId")
    public AjaxResult getHiddePlaceByParentId(HiddenPlace hiddenPlace)
    {
        List<HiddenPlace> list = hiddenPlaceService.selectHiddenPlaceList(hiddenPlace);
        return AjaxResult.success(list);
    }
}
